package com.web.controller;

import com.common.utils.Page;
import com.entity.*;
import com.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * gly控制器类
 */
@Controller
public class AdminController {
	// 依赖注入
	@Autowired
	private AdminService adminService;
	/**
	 * gly登录
	 */
	@RequestMapping(value = "/adminlogin.action", method = RequestMethod.POST)
	public String login(String adminname,String password, Model model,
						HttpSession session) {
		// 通过账号和密码查询gly
		String admin_name="";
		admin_name=adminname;
		Admin admin = adminService.findAdmin(admin_name, password);
		if(admin != null){
				// 将gly对象添加到Session
				session.setAttribute("Admin_SESSION", admin);
				// 跳转到主页面
				return "admin/index";
			}
			//session.setAttribute("msg","账号或密码错误，请重新输入");
			model.addAttribute("msg", "账号或密码错误，请重新输入！");
			// 返回到登录页面
			return "admin/login";
		}
	/**
	 * 分页查询
	 */
	@RequestMapping(value = "/admin/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows,String admin_name, 
			Model model,HttpServletRequest request,HttpSession session) {
		// 条件查询所有gly
		admin_name=request.getParameter("admin_name");
		if(admin_name==null) admin_name="";
		String admin_name1=request.getParameter("admin_name1");
		if(admin_name1==null) admin_name1="0";
		if(admin_name1.equals("1")) {
			String str=(String) session.getAttribute("admin_name");
			if(str!=null&&str!="") admin_name=str;
		}
		Page<Admin> admins = adminService.findAdminList(page, rows,admin_name);
		model.addAttribute("page", admins);
		model.addAttribute("admin_name", admin_name);
		return "admin/admin";
	}

	/**
	 * 通过id获取gly信息
	 */
	@RequestMapping("/admin/getAdminById.action")
	@ResponseBody
	public Admin getAdminById(Integer id) {
	    Admin admin = adminService.getAdminById(id);
	    return admin;
	}
	
	/**
	 * 创建gly
	 */
	@RequestMapping("/admin/create.action")
	@ResponseBody
	public String adminCreate(Admin admin) {
		if(admin.getAdmin_name()==""||admin.getAdmin_name()==null) {
			return "name";
		}
		if(admin.getPassword()==""||admin.getPassword()==null) {
			return "paw";
		}
		Admin adminname=adminService.findAdminname(admin.getAdmin_name());
		if(adminname!=null){
			return "Aname";
		}
	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = adminService.createAdmin(admin);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	/**
	 * 更新gly
	 */
	@RequestMapping("/admin/updateAdmin.action")
	@ResponseBody
	public String adminUpdate(Admin admin) {
		System.out.println(admin.getAdmin_name());
		if(admin.getAdmin_name()==""||admin.getAdmin_name()==null) {
			return "name";
		}
		Integer id=admin.getId();
		Admin admin1 = adminService.getAdminById(id);
		Admin adminname=adminService.findAdminname(admin.getAdmin_name());
		if(adminname!=null&&!admin1.getAdmin_name().equals(admin.getAdmin_name())){
			return "Aname";
		}
		int rows = adminService.updateAdmin(admin);
		if(rows > 0){
		    return "OK";
		}else{
		    return "FAIL";
		}
	}
	/**
	 * 更新gly
	 */
	@RequestMapping("/admin/update.action")
	@ResponseBody
	public String Update(Admin admin) {
		if(admin.getAdmin_name()==""||admin.getAdmin_name()==null) {
			return "name";
		}
		Integer id=admin.getId();
		Admin admin1 = adminService.getAdminById(id);
		Admin adminname=adminService.findAdminname(admin.getAdmin_name());
		if(adminname!=null&&!admin1.getAdmin_name().equals(admin.getAdmin_name())){
			return "Aname";
		}
		if(admin.getPassword()!=""&&admin.getPassword()!=null) {
			if(!admin.getPassword().equals(admin1.getPassword())) {
				return "paw";
			}
		}
		int rows = adminService.updateAdmin(admin);
		if(rows > 0){
		    return "OK";
		}else{
		    return "FAIL";
		}
	}
	/**
	 * 删除gly
	 */
	@RequestMapping("/admin/delete.action")
	@ResponseBody
	public String adminDelete(Integer id) {
	    int rows = adminService.deleteAdmin(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/adminlogout.action")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "redirect:adminlogin.action";
	}
	/**
	 * 向gly登陆页面跳转
	 */
	@RequestMapping(value = "/adminlogin.action", method = RequestMethod.GET)
	public String toLogin() {
		return "admin/login";
	}

}
